// 1  输入矩形层高，输出矩形
let fn = () => {
  let h = prompt('输入矩形层高:')
  h = Number.parseInt(h)
  if (Number.isNaN(h) || h < 1) {
    return console.log('矩形的层高必须是正整数')
  }
  let str = ''
  for (let i = 0; i < h; i++) {
    str += `<div class="item flex-center">`
    for (let j = 0; j < h; j++) {
      str += `<span>*</span>`
    }
    str += `</div>`
  }
  document.getElementsByClassName('fn')[0].innerHTML = str
}

// 2 输入三角形层高，输出三角形
let fn1 = () => {
  let h = prompt('输入三角形层高:')
  h = Number.parseInt(h)
  if (Number.isNaN(h) || h < 1) {
    return console.log('三角形的层高必须是正整数')
  }
  let str = ''
  for (let i = 0; i < h; i++) {
    str += `<div class="item flex-center">`
    for (let j = 0; j < 2 * i + 1; j++) {
      str += `<span>*</span>`
    }
    str += `</div>`
  }
  document.getElementsByClassName('fn1')[0].innerHTML = str
}

// 3 输入数字输出图形
let fn2 = () => {
  let n = prompt('输入一个正整数:')
  n = Number.parseInt(n)
  if (Number.isNaN(n) || n < 1) {
    // 程序手动抛出异常，强制终止程序
    throw '输入的值必须是正整数'
  }
  let str = ''
  for (let i = 0; i < n; i++) {
    // i_max= n-1
    str += `<div class="item flex-center">`
    let j = 0
    for (; j < 2 * i + 1; j++) {
      // j_max = 2 * i  = 2 * (n-1)
      str += `<span class='flex-center'>${i + 1}</span>  `
    }
    console.log(j)
    str += `</div>`
  }
  document.getElementsByClassName('fn2')[0].innerHTML = str
  // 最后一行有多少个span = 2*(n-1)+1个
  document.getElementsByClassName('fn2')[0].style.width = 30 * (2 * (n - 1) + 1) + 'px'
}

// 4 输入n个学生的成绩，如果低于80分，就终止统计，输出所有大于80分的学生个数 箭头函数
let fn3 = () => {
  let n = prompt('输入学生个数:')
  n = Number.parseInt(n)
  if (Number.isNaN(n) || n < 1) {
    throw '学生个数必须是正整数'
  }
  // 统计大于80分学生的个数
  let count = 0
  let score = null
  let arr = []
  for (let i = 0; i < n; i++) {
    score = prompt(`输入第${i + 1}位学生成绩:`)
    // 判断是否是>=0的数字
    score = Number.parseFloat(score)
    if (Number.isNaN(score) || score < 0) {
      throw '学生成绩是>=0的数字'
    }
    // 添加score元素到数组的最后一位
    arr.push(score)
    if (score < 80) {
      console.log('学生成绩低于80不予统计')
      continue
    }
    count++
  }
  document.getElementsByClassName('fn3')[0].innerHTML = `arr=${arr},大于80分的有count=${count}`
}

// 5 求 0 1~10  之间的所有偶数和
let fn4 = () => {
  let sum = 0
  for (let i = 0; i <= 10; i++) {
    if (i % 2) {
      continue
    }
    sum += i
  }
  document.getElementsByClassName('fn4')[0].innerHTML = `1-10偶数和sum=${sum}`
}

// 6 n层的金字塔
let fn5 = () => {
  let h = prompt('输入金字塔层高:')
  h = Number.parseInt(h)
  if (Number.isNaN(h) || h < 1) {
    return console.log('金字塔的层高必须是正整数')
  }
  let str = ''
  for (let i = 0; i < h; i++) {
    str += `<div class="item flex-center">`
    for (let j = 0; j <= i; j++) {
      str += `<span class="flex-center">*</span>`
    }
    str += `</div>`
  }
  document.getElementsByClassName('fn5')[0].innerHTML = str
}
fn5()
